package com.yumeisoft.aitables.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CodeExtractor {

    public static List<String> getFromAnswer(String llmAnswer, String type) {
        List<String> result = new ArrayList<>();
        
        if (llmAnswer == null || llmAnswer.isEmpty() || type == null || type.isEmpty()) {
            return result;
        }

        // 构建正则表达式，包含DOTALL和CASE_INSENSITIVE标志
        String regex = "(?is)```\\s*" + Pattern.quote(type) + "\\s*(.*?)\\s*```";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(llmAnswer);

        while (matcher.find()) {
            String content = matcher.group(1).trim();
            if (!content.isEmpty()) {
                result.add(content);
            }
        }

        return result;
    }

}